package com.zhuhao.export.exprotword;

import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author zhuhao
 * @Date 2021/11/26 16:16
 * @desc
 */
public class PoiExportWord {

    @Test
    public void test01() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "decode\r 换行\r得到换行\r双引号\'kkk\'");
        map.put("userName", "JueYue");
        map.put("sector", new Date());
        map.put("me", "JueYue");
        map.put("date", "2015-01-03");
        try {
            XWPFDocument doc = WordExportUtil.exportWord07(
                    "E:\\template.docx", map);
            FileOutputStream fos = new FileOutputStream("D:\\simple.docx");
            doc.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test02() {
        File file = new File("D:\\test");
        deleteDir(file);
    }

    /**
     * 递归删除目录下的所有文件及子目录下所有文件
     *
     * @param dir
     * @return
     */
    private static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            //递归删除目录中的子目录下
            String[] children = dir.list();
            for (int i = 0; i < (children != null ? children.length : 0); i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
    }
}
